﻿using K8.CBB;
using K8.MDCBB;
using KingerRobot.PCRBW.Biz.Enum;
using KingerRobot.PCRBW.Dao;
using KingerRobot.PCRBW.Spi;
using KingerRobot.PCRBW.SpiService;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace KingerRobot.PCRBW.Biz;
/// <summary>
/// 入库\出库
/// </summary>
public class GantryTaskBiz : K8.MDCBB.Biz
{
    #region 配置参数
    private ParamData Param { get; set; }

    class ParamData
    {
        /// <summary>
        ///钢丝龙门Id
        /// </summary>
        public long EquipId { get; set; }
        /// <summary>
        ///入库滑台1名称
        /// </summary>
        public string Sildetable1 { get; set; }
        /// <summary>
        ///入库滑台2名称
        /// </summary>
        public string Sildetable2 { get; set; }
        /// <summary>
        ///出库滑台1名称
        /// </summary>
        public string OutSildetable1 { get; set; }
        /// <summary>
        ///出库滑台2名称
        /// </summary>
        public string OutSildetable2 { get; set; }


    }
    #endregion

    #region PLC 通讯参数
    private ValueData Data { get; set; }
    class ValueData
    {
        #region 龙门点位
        /// <summary>
        /// 交互标志位
        /// </summary>
        public int GantryMark { get; set; }
        /// <summary>
        /// 清除反馈上位机指令标志
        /// </summary>
        public int ClearWCSFlag { get; set; }
        /// <summary>
        ///K=1 龙门运行任务；K=2 龙门空闲 ；K=3 龙门故障；K=4 龙门手动
        /// </summary>
        public int GantryStatus { get; set; }
        /// <summary>
        /// 龙门自动流程步
        /// </summary>
        public int GantryAutoStep { get; set; }
        /// <summary>
        /// 反馈报警字00
        /// </summary>
        public int AlarmToWCS00 { get; set; }
        /// <summary>
        /// 反馈报警字01
        /// </summary>
        public int AlarmToWCS01 { get; set; }
        /// <summary>
        /// 反馈报警字02
        /// </summary>
        public int AlarmToWCS02 { get; set; }
        /// <summary>
        /// 反馈报警字03
        /// </summary>
        public int AlarmToWCS03 { get; set; }
        /// <summary>
        /// 反馈报警字04
        /// </summary>
        public int AlarmToWCS04 { get; set; }
        /// <summary>
        /// 反馈报警字05
        /// </summary>
        public int AlarmToWCS05 { get; set; }
        /// <summary>
        /// 任务流水号—下发
        /// </summary>
        public int SerialNumber { get; set; }
        /// <summary>
        /// 指令码
        /// </summary>
        public int CommandCode { get; set; }
        /// <summary>
        /// 取站台号
        /// </summary>
        public int GetPlatformNumber { get; set; }
        /// <summary>
        /// 取X坐标
        /// </summary>
        public int GetXPosition { get; set; }
        /// <summary>
        /// 取Y坐标
        /// </summary>
        public int 取Y坐标 { get; set; }
        /// <summary>
        /// 取Z坐标
        /// </summary>
        public int GetZPosition { get; set; }
        /// <summary>
        /// 放站台号
        /// </summary>
        public int SetPlatformNumber { get; set; }
        /// <summary>
        /// 放X坐标
        /// </summary>
        public int SetXPosition { get; set; }
        /// <summary>
        /// 放Y坐标
        /// </summary>
        public int SetYPosition { get; set; }
        /// <summary>
        /// 放Z坐标
        /// </summary>
        public int SetZPosition { get; set; }
        /// <summary>
        /// 反馈指令流水号
        /// </summary>
        public int ReSerialNumber { get; set; }
        /// <summary>
        /// 空抓报警反馈 0代表正常，1代表龙门空抓报警
        /// </summary>
        public int ShortGrip { get; set; }


        #endregion

        #region 滑台

        /// <summary>
        /// 1#入库有货（滑台到位信号允许龙门取）
        /// </summary>
        public int AllowSignal1 { get; set; }
        /// <summary>
        /// 2#入库有货（滑台到位信号允许龙门取）
        /// </summary>
        public int AllowSignal2 { get; set; }
        /// <summary>
        /// 1#出库滑台状态
        /// </summary>
        public int OutSlideStatus1 { get; set; }
        /// <summary>
        /// 1#出库滑台任务流水号
        /// </summary>
        public int OutSerialNumber1 { get; set; }
        /// <summary>
        /// 1#入库滑台交互标志位
        /// </summary>
        public int OutCommandFlag1 { get; set; }

        /// <summary>
        ///  2#出库滑台状态
        /// </summary>
        public int OutSlideStatus2 { get; set; }
        /// <summary>
        /// 1#出库滑台任务流水号
        /// </summary>
        public int OutSerialNumber2 { get; set; }
        /// <summary>
        /// 2#入库滑台交互标志位
        /// </summary>
        public int OutCommandFlag2 { get; set; }
        /// <summary>
        /// 异常口滑台状态
        /// </summary>
        public int AbSlideStatus { get; set; }
        /// <summary>
        /// 异常口交互标志位
        /// </summary>
        public int AbCommandFlag { get; set; }
        /// <summary>
        /// 异常口滑台任务流水号
        /// </summary>
        public int AbSerialNumber { get; set; }
        /// <summary>
        /// 异常口滑台任务指令码
        /// </summary>
        public int AbCommandCode { get; set; }
        /// <summary>
        /// 异常口有货（请求龙门取货）0为不允许取，1为允许取
        /// </summary>
        public int AbHasGoodSignal { get; set; }
        #endregion
    }
    #endregion

    #region 缓存
    private CacheData Cache { get; set; }
    class CacheData
    {
        public int FinalTaskNo { get; set; }
    }
    #endregion

    #region 实时日志
    private StepData Step { get; set; }
    class StepData
    {
        private string _msg = string.Empty;
        public string StepMsg
        {
            get
            {
                return this._msg;
            }
            set
            {
                if (string.IsNullOrWhiteSpace(value))
                {
                    this._msg = string.Empty;
                    return;
                }
                this._msg = $"{System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}>>{value}";
            }
        }
    }
    private void StepMsg(string msg)
    {
        this.Step.StepMsg = msg;
        this.WriteLog(msg);
    }
    #endregion

    #region 文件日志（运行需要 Nlog 配置）
    private void WriteLog(string message)
    {
        K8.LSF.Client.Log(new K8.LSF.Log { Data = message });
    }
    #endregion

    /// <summary>
    /// 和 PLC 通讯对象
    /// </summary>
    private BizData BizData { get; set; }
    public int Execute(BizData writer)
    {
        // Excute 方法外使用 BizParam
        this.BizData = writer;
        // 返回参数
        var result = 0;
        int errorNo = 0; // 设备故障
        string errorMsg = string.Empty;


        #region 龙门故障报警  
        if (Data.GantryStatus == (int)EnumGantryStatus.Fault)
        {
            errorNo = Data.GantryStatus;
            errorMsg = QueryAlarmMessage();//将解析的错误编码返回
            this.StepMsg($"龙门出入库》龙门出入库》龙门{Param.EquipId} |故障Value.errorNo{Data.GantryStatus}");
        }
        Alarm.Instance.AddAlarm(Param.EquipId, errorNo, errorMsg);//更新报警 TODO  将报警信息记录到数据库中

        if (Data.GantryStatus == (int)EnumGantryStatus.Manual || Data.GantryStatus == (int)EnumGantryStatus.Fault)
        {
            this.StepMsg($"龙门出入库》龙门出入库》龙门[{Param.EquipId}]-[{Data.GantryStatus}]故障或者未进入自动");
            return result;
        }
        #endregion

        #region 龙门运行状态验证
        this.StepMsg($"龙门运行状态:{Data.GantryStatus}");
        if (Data.GantryStatus != (int)EnumGantryStatus.Free || Data.GantryMark != 2)
        {
            this.StepMsg($"龙门出入库》{Param.EquipId}号龙门不可写或非空闲状态 GantryStatus-{Data.GantryStatus}，GantryMark-{Data.GantryMark}");
            return result;
        }
        #endregion

        #region 结束任务

        //龙门下发任务完成写入1，PLC接收写入2|0
        if (Data.GantryMark == 1)
        {  //1表示PLC还未读取 禁止下步操作
            this.StepMsg($"龙门出入库》[{Param.EquipId}]龙门PLC还未读取");
            return result;
        }
        //龙门反馈任务完成，关闭任务
        if (Data.SerialNumber == Data.ReSerialNumber && Data.SerialNumber != Cache.FinalTaskNo && Data.SerialNumber > 0 && Data.ReSerialNumber > 0)
        {
            this.StepMsg($"龙门出入库》结束龙门[{Param.EquipId}]任务[{Data.ReSerialNumber}]");
            FinishGantryTask(writer, errorMsg);
        }
        #endregion

        #region 生成出入库任务
        var gantryTaskService = K8.RSF.Client.Create<IWarehousService>();
        //异常口 返库任务
        if (Data.AbSlideStatus == 1 && Data.AbHasGoodSignal == 1)
        {
            gantryTaskService.CreateWarehouseingTask(Param.EquipId, "");
        }
        if (Data.AllowSignal1 == 1)
        {
            gantryTaskService.CreateWarehouseingTask(Param.EquipId, "M1101");
        }
        if (Data.AllowSignal2 == 1)
        {
            gantryTaskService.CreateWarehouseingTask(Param.EquipId, "M1102");
        }

        #endregion

        #region 任务查询

        //根据指令查询可以执行的任务
        this.StepMsg($"[{Param.EquipId}]龙门开始执行");

        List<TaskCmdAndLocDto> taskAndCmd = gantryTaskService.GetTaskAndCmdBeanByCmdEquipId(Param.EquipId);

        this.StepMsg($"龙门出入库》查询任务数:{taskAndCmd?.Count}");
        if (taskAndCmd is null || taskAndCmd.Count == 0)
        {
            this.StepMsg($"龙门出入库》龙门未查询到指令和任务！设备编码：{Param.EquipId}");
            return result;
        }

        #endregion

        #region 任务下发
        foreach (var item in taskAndCmd)
        {
            #region 验证
            //滑台验证
            this.StepMsg($"任务类型:{item.WcsTaskCmd.TaskType.GetValueOrDefault()}");
            if (item.WcsTaskCmd.TaskType.GetValueOrDefault() == (int)EnumTaskType.Warehousing)
            {
                //写入返库标识
                if (item.WcsTaskCmd.Mark != 3)
                {
                    string locCode = item.GetRcsLocation.LocCode;
                    int signal = GetHasProduct(locCode);

                    if (signal != 1)
                    {
                        this.StepMsg($"龙门出入库》入库滑台{locCode}状态不允许取货");
                        continue;
                    }
                }
                if (item.WcsTaskCmd.Mark == 3 && Data.AbHasGoodSignal != 1)
                {
                    this.StepMsg($"龙门出入库》返库任务 异常口滑台状态不允许取货：{Data.AbHasGoodSignal}");
                    continue;
                }
            }

            #region 判断异常出库的时候异常口是否允许下发任务
            if (item.WcsTaskCmd.TaskType.GetValueOrDefault() == (int)EnumTaskType.Exwarehouse)
            {
                if (item.WcsTaskCmd.Mark == 2)//必须为人工出库
                {
                    //状态不为2 || 允许取料
                    if (Data.AbSlideStatus != 2 || Data.AbCommandFlag != 2)
                    {
                        this.StepMsg($"龙门出入库》异常口状态！【异常口状态:{Data.AbSlideStatus}》异常口标志位：{Data.AbCommandFlag}】");
                        continue;
                    }
                    if (Data.AbSlideStatus == 2 && Data.AbCommandFlag == 2)//状态和标志同时为2 下滑台任务，人工出库
                    {
                        var flag2 = this.BizData.WriteData(new
                        {
                            AbSerialNumber = item.WcsTaskCmd.TaskNo,//任务流水号
                            AbCommandCode = 2,
                        });
                        flag2 = this.BizData.WriteData(new
                        {
                            AbCommandFlag = 1
                        });

                        if (flag2 > 0)
                        {
                            this.StepMsg($"龙门出入库》人工出库 滑台下发成功！【异常口状态:{Data.AbSlideStatus}》异常口标志位：{Data.AbCommandFlag}】");
                        }
                        else
                        {
                            this.StepMsg($"龙门出入库》人工出库 滑台下发失败！【异常口状态:{Data.AbSlideStatus}》异常口标志位：{Data.AbCommandFlag}】》返回值：【{flag2}】");
                            continue;
                        }
                    }
                }
                if (item.WcsTaskCmd.Mark == 0)
                {
                    if (item.SetRcsLocation.LocCode == Param.OutSildetable1 && Data.OutSlideStatus1 != 2)
                    {
                        this.StepMsg($"龙门出入库》1号出库滑台状态未就位！【{Data.OutSlideStatus1}】");
                        continue;
                    }
                    if (item.SetRcsLocation.LocCode == Param.OutSildetable2 && Data.OutSlideStatus2 != 2)
                    {
                        this.StepMsg($"龙门出入库》2号出库滑台状态未就位！【{Data.OutSlideStatus2}】");
                        continue;
                    }
                }
            }
            #endregion
            #endregion

            #region 任务下发  
            // 下发
            int flag = 0;//返回任务标识

            //出入库滑台取放圈位置计算
            double up = 0;
            double innerdiameter = (double.Parse(item.WcsTaskCmd.Innerdiameter.ToString()) * 25.4) / 2;
            double down = 0;
            //库位取圈位置计算
            double upStore = 0;
            double downStore = 0;

            var settings = gantryTaskService.QuerySetting();
            if (settings == null)
            {
                this.StepMsg($"龙门出入库》设置的滑台和库位固定参数为空。");
                continue;
            }
            int StorageSlide1_Benchmarks = int.Parse(settings.FirstOrDefault(d => d.Code == ConstFields.StorageSlide1_Benchmarks).Value);
            int StorageSlide2_Benchmarks = int.Parse(settings.FirstOrDefault(d => d.Code == ConstFields.StorageSlide2_Benchmarks).Value);
            int OneLayer_Benchmarks = int.Parse(settings.FirstOrDefault(d => d.Code == ConstFields.OneLayer_Benchmarks).Value);
            int TwoLayer_Benchmarks = int.Parse(settings.FirstOrDefault(d => d.Code == ConstFields.TwoLayer_Benchmarks).Value);
            int ThrieLayer_Benchmarks = int.Parse(settings.FirstOrDefault(d => d.Code == ConstFields.ThrieLayer_Benchmarks).Value);
            int AbnormalSlide_Benchmarks = int.Parse(settings.FirstOrDefault(d => d.Code == ConstFields.AbnormalSlide_Benchmarks).Value);
            int OutboundSlide1_Benchmarks = int.Parse(settings.FirstOrDefault(d => d.Code == ConstFields.OutboundSlide1_Benchmarks).Value);
            int OutboundSlide2_Benchmarks = int.Parse(settings.FirstOrDefault(d => d.Code == ConstFields.OutboundSlide2_Benchmarks).Value);
            int Take_UpperLimit_Fixed = int.Parse(settings.FirstOrDefault(d => d.Code == ConstFields.Take_UpperLimit_Fixed).Value);
            int Slide_UpperLimit_Fixed = int.Parse(settings.FirstOrDefault(d => d.Code == ConstFields.Slide_UpperLimit_Fixed).Value);
            int Take_LowerLimit_Fixed = int.Parse(settings.FirstOrDefault(d => d.Code == ConstFields.Take_LowerLimit_Fixed).Value);
            int Slide_LowerLimit_Fixed = int.Parse(settings.FirstOrDefault(d => d.Code == ConstFields.Slide_LowerLimit_Fixed).Value);


            #region 入库任务
            //入库滑台取货
            if (item.GetRcsLocation.LocType == 1 && item.WcsTaskCmd.TaskType == 1 && item.WcsTaskCmd.Mark == 4 && item.GetRcsLocation.LocCode == "M1101")
            {
                up = StorageSlide1_Benchmarks - Slide_UpperLimit_Fixed;
                down = StorageSlide1_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(22, 2))) - Slide_LowerLimit_Fixed);
            }
            if (item.GetRcsLocation.LocType == 1 && item.WcsTaskCmd.TaskType == 1 && item.WcsTaskCmd.Mark == 4 && item.GetRcsLocation.LocCode == "M1102")
            {
                up = StorageSlide2_Benchmarks - Slide_UpperLimit_Fixed;
                down = StorageSlide2_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(22, 2))) - Slide_LowerLimit_Fixed);
            }
            //入库库位放货
            if (item.SetRcsLocation.LocType == 3 && item.WcsTaskCmd.TaskType == 1 && item.WcsTaskCmd.Mark == 4)
            {
                if (item.SetRcsLocation.AreaIndex == 1)
                {
                    upStore = OneLayer_Benchmarks - Take_UpperLimit_Fixed;
                    downStore = OneLayer_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - Take_LowerLimit_Fixed);
                }
                if (item.SetRcsLocation.AreaIndex == 2)
                {
                    upStore = TwoLayer_Benchmarks - Take_UpperLimit_Fixed;
                    downStore = TwoLayer_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - Take_LowerLimit_Fixed);
                }
                if (item.SetRcsLocation.AreaIndex == 3)
                {
                    upStore = ThrieLayer_Benchmarks - Take_UpperLimit_Fixed;
                    downStore = ThrieLayer_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - Take_LowerLimit_Fixed);
                }
            }

            #endregion

            #region 返库
            //返库 异常滑台取货
            if (item.WcsTaskCmd.TaskType == 1 && item.WcsTaskCmd.Mark == 3 && item.GetRcsLocation.LocType == 5 && item.GetRcsLocation.LocCode == "M2105")
            {
                up = AbnormalSlide_Benchmarks - Slide_UpperLimit_Fixed;
                down = AbnormalSlide_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(22, 2))) - Slide_LowerLimit_Fixed);
            }
            if (item.WcsTaskCmd.TaskType == 1 && item.WcsTaskCmd.Mark == 3 && item.SetRcsLocation.LocType == 3)
            {
                if (item.SetRcsLocation.AreaIndex == 1)
                {
                    upStore = OneLayer_Benchmarks - Take_UpperLimit_Fixed;
                    downStore = OneLayer_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - Take_LowerLimit_Fixed);
                }
                if (item.SetRcsLocation.AreaIndex == 2)
                {
                    upStore = TwoLayer_Benchmarks - Take_UpperLimit_Fixed;
                    downStore = TwoLayer_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - Take_LowerLimit_Fixed);
                }
                if (item.SetRcsLocation.AreaIndex == 3)
                {
                    upStore = ThrieLayer_Benchmarks - Take_UpperLimit_Fixed;
                    downStore = ThrieLayer_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - Take_LowerLimit_Fixed);
                }
            }
            #endregion

            #region 出库任务
            //出库库位
            if (item.GetRcsLocation.LocType == 3 && item.WcsTaskCmd.TaskType == 2 && item.WcsTaskCmd.Mark == 0)
            {
                if (item.GetRcsLocation.AreaIndex == 1)
                {
                    up = OneLayer_Benchmarks - Take_UpperLimit_Fixed;
                    down = OneLayer_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - Take_LowerLimit_Fixed);
                }
                if (item.GetRcsLocation.AreaIndex == 2)
                {
                    up = TwoLayer_Benchmarks - Take_UpperLimit_Fixed;
                    down = TwoLayer_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - Take_LowerLimit_Fixed);
                }
                if (item.GetRcsLocation.AreaIndex == 3)
                {
                    up = ThrieLayer_Benchmarks - Take_UpperLimit_Fixed;
                    down = ThrieLayer_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - Take_LowerLimit_Fixed);
                }
            }
            //出库滑台
            if (item.SetRcsLocation.LocType == 4 && item.WcsTaskCmd.TaskType == 2 && item.WcsTaskCmd.Mark == 0)
            {
                if (item.SetRcsLocation.LocCode == "M2103")
                {
                    upStore = OutboundSlide1_Benchmarks - Slide_UpperLimit_Fixed;
                    downStore = OutboundSlide1_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(22, 2))) - Slide_LowerLimit_Fixed);

                    var flag1 = this.BizData.WriteData(new
                    {
                        OutSerialNumber1 = item.WcsTaskCmd.TaskNo,//任务流水号
                        OutInnerdiameter1 = item.WcsTaskCmd.Innerdiameter,
                        OutCommandFlag1 = 1
                    });
                    if (flag1 <= 0)
                    {
                        this.StepMsg($"龙门出入库》写入滑台流水号失败！");

                        continue;
                    }
                }
                if (item.SetRcsLocation.LocCode == "M2104")
                {
                    upStore = OutboundSlide2_Benchmarks - Slide_UpperLimit_Fixed;
                    downStore = OutboundSlide2_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(22, 2))) - Slide_LowerLimit_Fixed);

                    var flag2 = this.BizData.WriteData(new
                    {
                        OutSerialNumber2 = item.WcsTaskCmd.TaskNo,//任务流水号
                        IOutInnerdiameter2 = item.WcsTaskCmd.Innerdiameter,
                        OutCommandFlag2 = 1
                    });
                    if (flag2 <= 0)
                    {
                        this.StepMsg($"龙门出入库》写入滑台流水号失败！");

                        continue;
                    }
                }
            }

            #endregion

            #region 人工出库
            //人工出库 异常滑台放货

            //出库 库位
            if (item.WcsTaskCmd.TaskType == 2 && item.GetRcsLocation.LocType == 3 && (item.WcsTaskCmd.Mark == 1 || item.WcsTaskCmd.Mark == 2))
            {
                if (item.GetRcsLocation.AreaIndex == 1)
                {
                    up = OneLayer_Benchmarks - Take_UpperLimit_Fixed;
                    down = OneLayer_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - Take_LowerLimit_Fixed);
                }
                if (item.GetRcsLocation.AreaIndex == 2)
                {
                    up = TwoLayer_Benchmarks - Take_UpperLimit_Fixed;
                    down = TwoLayer_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - Take_LowerLimit_Fixed);
                }
                if (item.GetRcsLocation.AreaIndex == 3)
                {
                    up = ThrieLayer_Benchmarks - Take_UpperLimit_Fixed;
                    down = ThrieLayer_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - Take_LowerLimit_Fixed);
                }
            }
            //出库异常滑台
            if (item.WcsTaskCmd.TaskType == 2 && item.SetRcsLocation.LocType == 5 && (item.WcsTaskCmd.Mark == 1 || item.WcsTaskCmd.Mark == 2))
            {
                upStore = AbnormalSlide_Benchmarks - Slide_UpperLimit_Fixed;
                downStore = AbnormalSlide_Benchmarks + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(22, 2))) - Slide_LowerLimit_Fixed);
            }
            #endregion

            #region 设定值

            //#region 入库任务
            ////入库滑台取货
            //if (item.GetRcsLocation.LocType == 1 && item.WcsTaskCmd.TaskType == 1 && item.WcsTaskCmd.Mark==4)
            //{
            //    up = 2098 - 60;
            //    down = 2098 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(22, 2))) - 123);
            //}
            ////入库库位放货
            //if (item.SetRcsLocation.LocType == 3 && item.WcsTaskCmd.TaskType == 1 && item.WcsTaskCmd.Mark == 4)
            //{
            //    if (item.SetRcsLocation.AreaIndex == 1)
            //    {
            //        upStore = 2406 - 60;
            //        downStore = 2406 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - 123);
            //    }
            //    if (item.SetRcsLocation.AreaIndex == 2)
            //    {
            //        upStore = 1599 - 60;
            //        downStore = 1599 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - 123);
            //    }
            //    if (item.SetRcsLocation.AreaIndex == 3)
            //    {
            //        upStore = 796 - 60;
            //        downStore = 796 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - 123);
            //    }
            //}

            //#endregion

            //#region 返库
            ////返库 异常滑台取货
            //if (item.WcsTaskCmd.TaskType == 1 && item.WcsTaskCmd.Mark == 3 && item.GetRcsLocation.LocType == 5)
            //{
            //    up = 2067 - 60;
            //    down = 2067 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(22, 2))) - 123);
            //}
            //if (item.WcsTaskCmd.TaskType == 1 && item.WcsTaskCmd.Mark == 3 && item.SetRcsLocation.LocType == 3)
            //{
            //    if (item.SetRcsLocation.AreaIndex == 1)
            //    {
            //        upStore = 2406 - 60;
            //        downStore = 2406 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - 123);
            //    }
            //    if (item.SetRcsLocation.AreaIndex == 2)
            //    {
            //        upStore = 1599 - 60;
            //        downStore = 1599 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - 123);
            //    }
            //    if (item.SetRcsLocation.AreaIndex == 3)
            //    {
            //        upStore = 796 - 60;
            //        downStore = 796 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - 123);
            //    }
            //} 
            //#endregion

            //#region 出库任务
            ////出库库位
            //if (item.GetRcsLocation.LocType == 3 && item.WcsTaskCmd.TaskType == 2 && item.WcsTaskCmd.Mark == 0)
            //{
            //    if (item.SetRcsLocation.AreaIndex == 1)
            //    {
            //        up = 2406 - 60;
            //        down = 2406 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - 123);
            //    }
            //    if (item.SetRcsLocation.AreaIndex == 2)
            //    {
            //        up = 1599 - 60;
            //        down = 1599 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - 123);
            //    }
            //    if (item.SetRcsLocation.AreaIndex == 3)
            //    {
            //        up = 796 - 60;
            //        down = 796 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - 123);
            //    }
            //}
            ////出库滑台
            //if (item.SetRcsLocation.LocType == 4 && item.WcsTaskCmd.TaskType == 2 && item.WcsTaskCmd.Mark == 0)
            //{
            //    if (item.SetRcsLocation.PlcCode == "8101")
            //    {
            //        upStore = 2022 - 60;
            //        downStore = 2022 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(22, 2))) - 123);
            //    }
            //    if (item.SetRcsLocation.PlcCode == "8102")
            //    {
            //        upStore = 2025 - 60;
            //        downStore = 2025 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(22, 2))) - 123);
            //    }

            //}

            //#endregion

            //#region 人工出库
            ////人工出库 异常滑台放货

            ////入库库位放货
            //if (item.WcsTaskCmd.TaskType == 2 && item.GetRcsLocation.LocType ==3 && (item.WcsTaskCmd.Mark == 1 || item.WcsTaskCmd.Mark == 2))
            //{
            //    if (item.GetRcsLocation.AreaIndex == 1)
            //    {
            //        up = 2406 - 60;
            //        down = 2406 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - 123);
            //    }
            //    if (item.GetRcsLocation.AreaIndex == 2)
            //    {
            //        up = 1599 - 60;
            //        down = 1599 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - 123);
            //    }
            //    if (item.GetRcsLocation.AreaIndex == 3)
            //    {
            //        up = 796 - 60;
            //        down = 796 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(25, 2))) - 123);
            //    }
            //}

            //if (item.WcsTaskCmd.TaskType == 2  && item.SetRcsLocation.LocType == 5 && (item.WcsTaskCmd.Mark == 1||  item.WcsTaskCmd.Mark == 2))
            //{
            //    upStore = 2067 - 60;
            //    downStore = 2067 + (Math.Sqrt((Math.Pow(innerdiameter, 2) - Math.Pow(22, 2))) - 123);
            //}
            //#endregion 
            #endregion

            var data = new
            {
                SerialNumber = item.WcsTaskCmd.TaskNo,//任务流水号
                CommandCode = this.QueryTaskType(item.WcsTaskCmd.TaskType.GetValueOrDefault()),//命令
                GetPlatformNumber = item.GetRcsLocation.PlcCode,
                GetXPosition = item.GetRcsLocation.AxisX,//取料X
                GetYPosition = item.GetRcsLocation.AxisY,//取料Y
                GetZPosition = up,//取料Z 上限
                TakeZPosition = down,//取料Z 下限
                SetPlatformNumber = item.SetRcsLocation.PlcCode,
                SetXPosition = item.SetRcsLocation.AxisX,//放料X
                SetYPosition = item.SetRcsLocation.AxisY,//放料Y
                SetZPosition = upStore,//放料Z 上限
                DownZPosition = downStore,//放料Z 下限
                Innerdiameter= item.WcsTaskCmd.Innerdiameter
            };

            flag = this.BizData.WriteData(data);
            this.StepMsg($"龙门出入库》任务下发PLC:{flag},下发数据:{data.ToJson()}");
            if (flag > 0)
            {
                this.StepMsg($"龙门出入库》下发任务标识:开始");
                flag = this.BizData.WriteData(new
                {
                    GantryMark = 1
                });
                this.StepMsg($"龙门出入库》下发任务标识:{flag}");
                if (flag > 0)
                {
                    this.StepMsg($"龙门任务>下发PLC成功,龙门：{Param.EquipId};下发类型：{item.WcsTaskCmd.TaskType}；任务：{item.WcsTaskCmd.TaskId}");
                    flag = gantryTaskService.ExecuteTask(item.WcsTaskCmd.TargetLoc.GetValueOrDefault(), item.WcsTaskCmd.TaskNo.GetValueOrDefault());
                    this.StepMsg($"龙门出入库》任务下发3:{flag}");
                    if (flag > 0)
                    {
                        this.StepMsg("龙门出入库》龙门任务>下发成功!");
                        break;
                    }
                }
            }
            
            #endregion
        }
        #endregion

        return result;
    }

    private int FinishGantryTask(BizData writer, string errorMsg)
    {
        int finishResult = 0;
        this.StepMsg($"{Param.EquipId}龙门,开始完成任务");
        var gantryTaskService = K8.RSF.Client.Create<IWarehousService>();
        //结束上次入/出库任务
        finishResult = gantryTaskService.FinishTask(Data.SerialNumber);

        if (finishResult > 0)
        {
            Cache.FinalTaskNo = Data.ReSerialNumber;//流水号缓存
        }
        this.StepMsg($"[{Param.EquipId}]龙门,完成任务结束");
        return finishResult;
    }

    public int QueryTaskType(int taskType)
    {
        int result = 0;
        switch (taskType)
        {
            case (int)EnumTaskType.Warehousing:
                result = (int)EnumCmdType.Warehousing;
                break;
            case (int)EnumTaskType.Exwarehouse:
                result = (int)EnumCmdType.ExWarehouse;
                break;
            default:
                break;
        }
        return result;
    }

    public int GetHasProduct(string code)
    {
        int result = 0;
        if (Param.Sildetable1 == code)
        {
            this.StepMsg($"滑台1 状态:{Data.AllowSignal1}");
            result = Data.AllowSignal1;
        }
        if (Param.Sildetable2 == code)
        {
            this.StepMsg($"滑台2 状态:{Data.AllowSignal2}");
            result = Data.AllowSignal2;
        }
        return result;
    }

    public string QueryAlarmMessage()
    {
        string result = string.Empty;
        List<string> list = new List<string>();

        list.Add(Data.AlarmToWCS00.ToString());
        list.Add(Data.AlarmToWCS01.ToString());
        list.Add(Data.AlarmToWCS02.ToString());
        list.Add(Data.AlarmToWCS03.ToString());
        list.Add(Data.AlarmToWCS04.ToString());
        list.Add(Data.AlarmToWCS05.ToString());
        return list.ToJson();
    }
}
